<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/8/19
 * Time: 15:10
 * 司机路线图
 */
namespace app\driver\controller;
use think\Db;
class Route extends Base {
    /**
     * 异地登录验证
     * @return string
     */
//    public function __construct()
//    {
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == 112){
//            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
//            exit;
//        }elseif($auth == 110){
//            echo json_encode(['status' => 302, 'msg' => $this->frost]);
//            exit;
//        }
//    }
        //配货大厅
    public function index(){
        extract(input());
        $num = Db::table('up_route')->where('user_id',$user_id)->count();
        $res = Db::table('up_route')->where('user_id',$user_id)->select();
        foreach ($res as $key=>&$vo){
            if($vo['go_status']==-1&&$vo['to_status']==-1){//出发地和目的地都是全国
                $vo['go_sheng_name'] = '全国';
                $vo['to_sheng_name'] = '全国';
                $where['a.order_type'] = array('not in',array(3,4));
                $where['a.order_status'] = 2;
                $count = Db::table('up_order')->alias('a')
                    ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                    ->join('up_area ua','ua.district_id = a.order_go_sheng')
                    ->join('up_area ua1','ua1.district_id = a.order_go_shi')
                    ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
                    ->join('up_area ua3','ua3.district_id = a.order_to_shi')
                    ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
                    ->where($where)
                    ->count();
                $vo['count'] = $count;
            }elseif ($vo['go_status']==-1&&$vo['to_status']==-2){//出发地全国,目的地全省
                $vo['go_sheng_name'] = '全国';
                $vo['to_sheng_name'] = Db::table('up_area')->where('district_id',$vo['to_sheng'])->value('district');
                $where['a.order_type'] = array('not in',array(3,4));
                $where['a.order_status'] = 2;
                $count = Db::table('up_order')->alias('a')
                    ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                    ->join('up_area ua','ua.district_id = a.order_go_sheng')
                    ->join('up_area ua1','ua1.district_id = a.order_go_shi')
                    ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
                    ->join('up_area ua3','ua3.district_id = a.order_to_shi')
                    ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
                    ->where('a.order_to_sheng',$vo['to_sheng'])
                    ->where($where)
                    ->count();
                $vo['count'] = $count;


            }elseif ($vo['go_status']==-2&&$vo['to_status']==-1){//出发地全省，目的地全国
                $vo['go_sheng_name'] = Db::table('up_area')->where('district_id',$vo['go_sheng'])->value('district');
                $vo['to_sheng_name'] = '全国';
                $where['a.order_type'] = array('not in',array(3,4));
                $where['a.order_status'] = 2;
                $count = Db::table('up_order')->alias('a')
                    ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                    ->join('up_area ua','ua.district_id = a.order_go_sheng')
                    ->join('up_area ua1','ua1.district_id = a.order_go_shi')
                    ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
                    ->join('up_area ua3','ua3.district_id = a.order_to_shi')
                    ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
                    ->where('a.order_go_sheng',$vo['go_sheng'])
                    ->where($where)
                    ->count();
                $vo['count'] = $count;
            }else{
                $vo['go_sheng_name'] = Db::table('up_area')->where('district_id',$vo['go_sheng'])->value('district');
                $vo['to_sheng_name'] = Db::table('up_area')->where('district_id',$vo['to_sheng'])->value('district');
                $where['a.order_type'] = array('not in',array(3,4));
                $where['a.order_status'] = 2;
                $count = Db::table('up_order')->alias('a')
                    ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                    ->join('up_area ua','ua.district_id = a.order_go_sheng')
                    ->join('up_area ua1','ua1.district_id = a.order_go_shi')
                    ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
                    ->join('up_area ua3','ua3.district_id = a.order_to_shi')
                    ->join('up_u_user uuu','uuu.user_id = a.order_jms_user')
                    ->where('a.order_to_sheng',$vo['to_sheng'])
                    ->where('a.order_go_sheng',$vo['go_sheng'])
                    ->where($where)
                    ->count();
                $vo['count'] = $count;
            }
        }
        $list['num'] = $num;
        $list['data'] = $res;
        return json(['status'=>200,'msg'=>$list]);
    }
    //路线订单
    public function order(){
        extract(input());
        $route_id = input('param.route_id');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $res = Db::table('up_route')->where('route_id',$route_id)->find();
        if($res['go_status']==-1&&$res['to_status']==-1){//出发地和目的地都是全国
            $data['go_sheng_name'] = '全国';
            $data['to_sheng_name'] = '全国';
            $where['a.order_type'] = array('not in',array(3,4));
            $where['a.order_status'] = 2;
            $list = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user','left')
                ->limit($limit,$num)
                ->where($where)
                ->order('a.order_fb_time desc')
                ->field('a.order_num,a.order_intro,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,uuu.user_phone,uuu.user_username,uuu.user_real_name')
                ->select();
        }elseif ($res['go_status']==-1&&$res['to_status']==-2){//出发地全国,目的地全省
            $data['go_sheng_name'] = '全国';
            $data['to_sheng_name'] = Db::table('up_area')->where('district_id',$res['to_sheng'])->value('district');
            $where['a.order_type'] = array('not in',array(3,4));
            $where['a.order_status'] = 2;
            $where['a.order_to_sheng'] = $res['to_sheng'];
            $list = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user','left')
                ->limit($limit,$num)
                ->where($where)
                ->order('a.order_fb_time desc')
                ->field('a.order_num,a.order_intro,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,uuu.user_phone,uuu.user_username,uuu.user_real_name')
                ->select();
        }elseif ($res['go_status']==-2&&$res['to_status']==-1){//出发地全省，目的地全国
            $data['go_sheng_name'] = Db::table('up_area')->where('district_id',$res['go_sheng'])->value('district');
            $data['to_sheng_name'] = '全国';
            $where['a.order_type'] = array('not in',array(3,4));
            $where['a.order_status'] = 2;
            $where['a.order_go_sheng'] = $res['go_sheng'];
            $list = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user','left')
                ->limit($limit,$num)
                ->where($where)
                ->order('a.order_fb_time desc')
                ->field('a.order_num,a.order_intro,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,uuu.user_phone,uuu.user_username,uuu.user_real_name')
                ->select();
        }else{
            $data['go_sheng_name'] = Db::table('up_area')->where('district_id',$res['go_sheng'])->value('district');
            $data['to_sheng_name'] = Db::table('up_area')->where('district_id',$res['to_sheng'])->value('district');
            $where['a.order_type'] = array('not in',array(3,4));
            $where['a.order_status'] = 2;
            $where['a.order_go_sheng'] = $res['go_sheng'];
            $where['a.order_to_sheng'] = $res['to_sheng'];
            $list = Db::table('up_order')->alias('a')
                ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
                ->join('up_car_zxlx ucz','ucz.zxlx_id = a.zxlx_id','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->join('up_u_user uuu','uuu.user_id = a.order_jms_user','left')
                ->limit($limit,$num)
                ->where($where)
                ->order('a.order_fb_time desc')
                ->field('a.order_num,a.order_intro,ucz.zxlx_name,a.order_goods_m,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi,uuu.user_image,uuu.user_phone,uuu.user_username,uuu.user_real_name')
                ->select();
        }
        $result['data'] = $data;
        $result['list'] = $list;
        return json(['status'=>200,'msg'=>$result]);
    }

    public function addRoute(){
        extract(input());
        $go_sheng = input('param.go_sheng');
        $go_shi = input('param.go_shi');
        $go_qu = input('param.go_qu');
        $to_sheng = input('param.to_sheng');
        $to_shi = input('param.to_shi');
        $to_qu = input('param.to_qu');
        $go_status = input('param.go_status');
        $to_status = input('param.to_status');
        $data['go_sheng'] = $go_sheng;
        $data['go_shi'] = $go_shi;
        $data['go_qu'] = $go_qu;
        $data['to_sheng'] = $to_sheng;
        $data['to_shi'] = $to_shi;
        $data['to_qu'] = $to_qu;
        $data['go_status'] = $go_status;
        $data['to_status'] = $to_status;
        $data['route_time'] = date('Y-m-d H:i:s');
        $data['user_id'] = $user_id;
        $res = Db::table('up_route')->insertGetId($data);
        if($res){
            return json(['status'=>200,'msg'=>"成功"]);
        }else{
            return json(['status'=>100,'msg'=>'失败']);
        }
    }
    //删除路线
    public function delRoute(){
        $route_id = input('param.route_id');
        $res = Db::table('up_route')->where('route_id',$route_id)->delete();
        if($res){
            return json(['status'=>200,'msg'=>"成功"]);
        }else{
            return json(['status'=>200,'msg'=>'失败']);
        }
    }

    public function updateDetail(){
        extract(input());
        $route_id = input('param.route_id');
        $res = Db::table('up_route')->where('route_id',$route_id)->find();
        $district1 = Db::table('up_area')->where('district_id',$res['go_sheng'])->value('district');
        $district2 = Db::table('up_area')->where('district_id',$res['go_shi'])->value('district');
        $district3 = Db::table('up_area')->where('district_id',$res['go_qu'])->value('district');
        $district4 = Db::table('up_area')->where('district_id',$res['to_sheng'])->value('district');
        $district5 = Db::table('up_area')->where('district_id',$res['to_shi'])->value('district');
        $district6 = Db::table('up_area')->where('district_id',$res['to_qu'])->value('district');
        $res['district1'] =$district1;
        $res['district2'] =$district2;
        $res['district3'] =$district3;
        $res['district4'] =$district4;
        $res['district5'] =$district5;
        $res['district6'] =$district6;
        return json(['status'=>200,'msg'=>$res]);
    }

    public function updateRoute(){
        extract(input());
        $route_id = input('param.route_id');
        $go_sheng = input('param.go_sheng');
        $go_shi = input('param.go_shi');
        $go_qu = input('param.go_qu');
        $to_sheng = input('param.to_sheng');
        $to_shi = input('param.to_shi');
        $to_qu = input('param.to_qu');
        $go_status = input('param.go_status');
        $to_status = input('param.to_status');
        $data['go_sheng'] = $go_sheng;
        $data['go_shi'] = $go_shi;
        $data['go_qu'] = $go_qu;
        $data['to_sheng'] = $to_sheng;
        $data['to_shi'] = $to_shi;
        $data['to_qu'] = $to_qu;
        $data['go_status'] = $go_status;
        $data['to_status'] = $to_status;
        Db::table('up_route')->where('route_id',$route_id)->update($data);
        return json(['status'=>200,'msg'=>"成功"]);
    }
    public function province(){
        $res = Db::table('up_area')->where('level',1)->select();
        return json(['status'=>200,'msg'=>$res]);
    }

    public function city(){
        $id  = input('param.id');
        $res = Db::table('up_area')->where('pid',$id)->select();
        return json(['status'=>200,'msg'=>$res]);
    }

    public function area(){
        $id  = input('param.id');
        $res = Db::table('up_area')->where('pid',$id)->select();
        return json(['status'=>200,'msg'=>$res]);
    }
}